<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;

class MyImgController extends Controller
{
    public function index()
    {
        //查询数据（myimg）表
        $result = DB::table('dyimg')->paginate(4);
        if (empty($result)) {
            return view('admin/myimg');
        } else {
            //跳转到首页
            return view('admin/myimg', ['result' => $result]);
        }

    }

    public function add(Request $request)
    {
        //判断是否上传图片
        if (empty($request->file())) {
            return redirect('/myphoto');
        }
        //session值ID值
        $id = session('userid');
        if (empty($id)){
            return view('/home/myphoto');
        }
        //有值的话接收文件
        $file = $request->File();
        $key = key($file);
        if ($_FILES[$key]['error'] > 0) {

            switch ($_FILES[$key]['error']) {
                case '1':
                    $msg = '孩子, 回家重新上传, 文件太大了, 不要超过2M';
                    return back();
                    break;
                case '2':
                    $msg = '请打开F12, 删除 MAX_FILE_SIZE , 即可正常上传';
                    return back();
                    break;
                case '3':
                    $msg = '请查看网线是否连接好';
                    return back();
                    break;
                case '4':
                    $msg = '请不要调戏我, 上传一个给我';
                    return back();
                    break;
                case '6':
                    $msg = '请联系网管, 晚上啥时候约一下, 连个目录都不给我';
                    return back();
                    break;
                case '7':
                    $msg = '再联系网管, 你想什么时候死, 说一声, 连个权限都不给我';
                    return back();
                    break;
            }
        }
        if (!is_uploaded_file($_FILES[$key]['tmp_name'])) {
            return back();
        }

        // 判断文件类型
        $ext = $request->file('dm_img')->getClientOriginalExtension();

        if (!in_array($ext, array('jpg', 'png', 'jpeg'))) {
            //('不合法的文件类型');
            return back();
        }

        // 移动文件到指定位置

        $path = './uploads/' . date('Y/m/d');
        $name = date('Ymd') . uniqid();
        $ext = $request->file('dm_img')->getClientOriginalExtension();
        $filename = $name . '.' . $ext;//文件名
        $request->file('dm_img')->move($path, $filename);
        $uimg = $path.'/'.$filename;
        $img = ltrim($uimg,'.');
        //处理数据存入数据库
        $data = array(
            'dm_user' => $id,      //用户ID
            'dm_img' => $img,//图片名
        );
        $list = DB::table('dyimg')->insert($data);
        if ($list) {
            $result = DB::table('dyimg')->get();
            foreach ($result as $k => $v) {
                $v = $v;
            }
            $dm_id = $v->id; //dm_id的值
            $ok = DB::update("update `dyimg` set `dm_id` = $dm_id where id=" . $dm_id);
            if ($ok) {
                return redirect('/myphoto');
            }
        }
    }

    //ajax删除
    public function del($id)
    {
        //接收数据
        $id = $id;

        //删除之前把(userole)里的数据也删除

        $result = DB::delete('delete from `dyimg` where id=' . $id);

        if ($result) {
            echo 1;
        } else {
            echo 2;
        }

    }

    //查看前台页面
    public function look()
    {
        //session值ID值
        $id = session('userid');
        if (empty($id)){
        return view('/home/myphoto');
        }

        //查询数据
        $result = DB::select("select `id`,`dm_id`,`dm_img` from `dyimg` where `dm_user`= $id");
        //拼接路径return到前台
        //2017062759524c0f07b27.jpg
        $img = [];
        foreach ($result as $v) {
            $img[] = array(
                'img' => $v->dm_img,
                'id' => $v->id
            );
        }
        if (empty($img)) {
            return view('/home/myphoto');
        }

        if ($result) {
            return view('/home/myphoto', ['img' => $img]);
        }
    }


    //前台删除
    public function delete($id)
    {
        //接收id
        $id= $id;
        //执行操作删除
        $result = DB::delete("delete from `dyimg` where id=".$id);
        if($result){
            return redirect('/myphoto');
        }
    }




    //根据当前session 下的 ID 查询所需要的数据
    public function cha()
    {


       // echo 111;die;
        //获取session ID
        $id = session('userid');
        //根据当前 session ID 获取用户昵称 头像 简介
        $res = DB::table('user')
            ->join('detailed','user.id','=','detailed.d_id')
            ->where('user.id','=',$id)
            ->select('user.u_name','user.u_img','user.u_member','user.u_v','detailed.d_biref')->get();


        return response()->json($res);

        //var_dump($res);die;

       //

    }

}
